package hr.fer.zpr.nmbp.project1.dao;

import hr.fer.zpr.nmbp.project1.model.Bolest;
import hr.fer.zpr.nmbp.project1.model.Bolnica;
import hr.fer.zpr.nmbp.project1.model.Dijagnoza;
import hr.fer.zpr.nmbp.project1.model.Karton;
import hr.fer.zpr.nmbp.project1.model.Lijecnik;
import hr.fer.zpr.nmbp.project1.model.Pacijent;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.constraints.UniqueFieldValueConstraint;

public class DAOFactory {

	public static final String DB4OFILENAME = "appdb.db4o";

	private static ObjectContainer dbInstance;

	public static void init() {
		EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
		
		// Indexes
		config.common().objectClass(Bolest.class).objectField("sifra").indexed(true);
		config.common().objectClass(Bolnica.class).objectField("sifra").indexed(true);
		config.common().objectClass(Dijagnoza.class).objectField("sifra").indexed(true);
		config.common().objectClass(Karton.class).objectField("pacijent").indexed(true);
		config.common().objectClass(Lijecnik.class).objectField("sifra").indexed(true);
		config.common().objectClass(Pacijent.class).objectField("mbo").indexed(true);
		
		// Unique field constraints
		config.common().add( new UniqueFieldValueConstraint(Bolest.class, "sifra"));
		config.common().add( new UniqueFieldValueConstraint(Bolnica.class, "sifra"));
		config.common().add( new UniqueFieldValueConstraint(Dijagnoza.class, "sifra"));
		config.common().add( new UniqueFieldValueConstraint(Karton.class, "pacijent"));
		config.common().add( new UniqueFieldValueConstraint(Lijecnik.class, "sifra"));
		config.common().add( new UniqueFieldValueConstraint(Pacijent.class, "mbo"));
		
		// Foreign key constraint
		config.common().objectClass(Bolnica.class).cascadeOnUpdate(true);
		config.common().objectClass(Bolnica.class).updateDepth(6);
		
		config.common().objectClass(Dijagnoza.class).cascadeOnUpdate(true);
		config.common().objectClass(Dijagnoza.class).updateDepth(5);
		
		config.common().objectClass(Lijecnik.class).cascadeOnUpdate(true);
		config.common().objectClass(Lijecnik.class).updateDepth(5);
		
		config.common().objectClass(Pacijent.class).cascadeOnUpdate(true);
		config.common().objectClass(Pacijent.class).updateDepth(4);
		config.common().objectClass(Pacijent.class).objectField("karton").cascadeOnDelete(true);
		
		config.common().objectClass(Karton.class).cascadeOnUpdate(true);
		config.common().objectClass(Karton.class).updateDepth(4);
		config.common().objectClass(Karton.class).objectField("dijagnoze").cascadeOnDelete(true);
		
		dbInstance = Db4oEmbedded.openFile(config, DB4OFILENAME);
	}

	public static void close(){
		dbInstance.close();
	}	

	public static BolestDAO getBolestDAO() {
		return new BolestDAO(dbInstance);
	}	

	public static BolnicaDAO getBolnicaDAO() {
		return new BolnicaDAO(dbInstance);
	}	

	public static DijagnozaDAO getDijagnozaDAO() {
		return new DijagnozaDAO(dbInstance);
	}	

	public static KartonDAO getKartonDAO()	{
		return new KartonDAO(dbInstance);
	}	

	public static LijecnikDAO getLijecnikDAO()	{
		return new LijecnikDAO(dbInstance);
	}	

	public static PacijentDAO getPacijentDAO()	{
		return new PacijentDAO(dbInstance);
	}
}
